<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/banner.css">
</head>

<body>
    <div class="mybanner">
        <ul class="hot">
            <li>
                <a href=""> <img src="images/banner1.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner2.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner3.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner4.jpg" alt=""> </a>
            </li>
        </ul>
        <div class='arrow'>
            <span class="prev"> < </span>
            <span class="next"> > </span>
        </div>
        <ul class="circle-list">
            <li class="circle active" data-n="0"></li>
            <li class="circle" data-n="1"></li>
            <li class="circle" data-n="2"></li>
            <li class="circle" data-n="3"></li>
        </ul>
    </div>
    <script>
        var ul = document.querySelector('.hot');
        var lis = ul.children; // 使用 children 而不是 childNodes
        var dd = document.querySelector('.circle-list');
        var dds = dd.children;
        var left = document.querySelector('.prev');
        var right = document.querySelector('.next');
        var currentIndex = 0;

        function lunbo() {
            // 隐藏所有图片
            for (var i = 0; i < lis.length; i++) {
                lis[i].style.display = 'none';
                dds[i].className = 'circle';
            }

            // 显示当前图片
            lis[currentIndex].style.display = 'block';
            dds[currentIndex].className = "circle active";


            // 更新索引
            currentIndex = (currentIndex + 1) % lis.length;

            // 3秒后再次调用
            setTimeout(lunbo, 3000);
        }

        function next() {
            for (var i = 0; i < lis.length; i++) {
                lis[i].style.display = 'none';
                dds[i].className = 'circle';
            }
            currentIndex = (currentIndex + 1) % lis.length;
            lis[currentIndex].style.display = 'block';
            dds[currentIndex].className = "circle active";
        }

        function prev() {
            // 隐藏所有图片
            for (var i = 0; i < lis.length; i++) {
                lis[i].style.display = 'none';
                dds[i].className = 'circle';
            }

            // 更新索引并显示前一张图片
            currentIndex = (currentIndex - 1 + lis.length) % lis.length;
            lis[currentIndex].style.display = 'block';
            dds[currentIndex].className = "circle active";
        }
        // 开始轮播
        lunbo();


        right.addEventListener('click', next);
        // 为 left 按钮添加点击事件
        left.addEventListener('click', prev);

        // 添加点击导航圆点显示对应图片的功能
        for (var i = 0; i < dds.length; i++) {
            dds[i].onclick = function() {
                var j = this.getAttribute('data-n');
                for (var i = 0; i < dds.length; i++) {
                    lis[i].style.display = 'none';
                    dds[i].className = 'circle';
                }
                lis[j].style.display = 'block'; // 显示对应的图片
                dds[j].className = 'circle active';
            }
        }
    </script>
</body>

</html>